#import	"SpinView.h"
#import	"Thinker.h"

#import	<strings.h>
#import	<defaults/defaults.h>
#import	<sys/param.h>

#import	<appkit/Application.h>
#import	<appkit/TextField.h>
#import	<appkit/Slider.h>
#import	<appkit/Panel.h>
	
/**********************************************************************/

static	char	tnum[ 20 ];
	
/**********************************************************************/

@implementation	SpinView(inspect)

/**********************************************************************/

- inspector : sender
{
	char	buf[MAXPATHLEN];

	if( !( inspectPanel ) )
	{
		sprintf( buf, "%s/Spin.nib", [ sender moduleDirectory:"Spin" ] );
		
		inspectPanel = [ NXApp loadNibFile:buf owner:self withNames:NO ];
	
		[ self connectInspector ];
	}
	
	return [ mainInspectBox contentView ];
}

/**********************************************************************/

- ( void ) connectInspector
{
	[ angleSlider setMaxValue : MAXANGLEINC ];
	[ angleSlider setMinValue : MINANGLEINC ];
	[ angleSlider setIntValue : CurAngleInc ];
	
	[ angleField setIntValue : CurAngleInc ];
	
	[ numSpinSlider setMaxValue : MAXNUMSPINS ];
	[ numSpinSlider setMinValue : MINNUMSPINS ];
	[ numSpinSlider setIntValue : CurNumSpins ];
	
	[ numSpinField setIntValue : CurNumSpins ];
	
	[ radiusSlider setMaxValue : MAXRADIUS ];
	[ radiusSlider setMinValue : MINRADIUS ];
	[ radiusSlider setIntValue : CurRadius ];
	
	[ radiusField setIntValue : CurRadius ];
	
	[ tailSlider setMaxValue : MAXTAILLEN ];
	[ tailSlider setMinValue : MINTAILLEN ];
	[ tailSlider setIntValue : CurTailLen ];
	
	[ tailField setIntValue : CurTailLen ];
	
	[ xIncSlider setMaxValue : MAXXINC ];
	[ xIncSlider setMinValue : MINXINC ];
	[ xIncSlider setFloatValue : CurXInc ];

	[ xIncField setFloatingPointFormat : NO left : 2 right : 1 ];
	[ xIncField setFloatValue : CurXInc ];
	
	[ yIncSlider setMaxValue : MAXYINC ];
	[ yIncSlider setMinValue : MINYINC ];
	[ yIncSlider setFloatValue : CurYInc ];

	[ yIncField setFloatingPointFormat : NO left : 2 right : 1 ];
	[ yIncField setFloatValue : CurYInc ];
}
	
/**********************************************************************/

- setAngle : sender
{
	int	angleInc;
	
	angleInc = [ sender intValue ];

	if( sender == angleSlider )
		[ angleField setIntValue : angleInc ];
	else
	{
		if( angleInc > MAXANGLEINC )
		{
			angleInc = MAXANGLEINC;
			[ angleField setIntValue : angleInc ];
		}
		else if( angleInc < MINANGLEINC )
		{
			angleInc = MINANGLEINC;
			[ angleField setIntValue : angleInc ];
		}
		
		[ angleSlider setIntValue : angleInc ];
	}
	
	sprintf( tnum, "%3d", angleInc );
	NXWriteDefault( OWNER, "AngleIncrement", tnum );
	
	CurAngleInc = angleInc;
	
	[ self display ];
	[ self resetSpinners ];
	
	return self;
}
	
/**********************************************************************/

- setXInc : sender
{
	float	xInc;
	
	xInc = [ sender floatValue ];

	if( sender == xIncSlider )
		[ xIncField setFloatValue : xInc ];
	else
	{
		if( xInc > MAXXINC )
		{
			xInc = MAXXINC;
			[ xIncField setFloatValue : xInc ];
		}
		else if( xInc < MINXINC )
		{
			xInc = MINXINC;
			[ xIncField setFloatValue : xInc ];
		}
		
		[ xIncSlider setFloatValue : xInc ];
	}
	
	sprintf( tnum, "%6.3f", xInc );
	NXWriteDefault( OWNER, "XIncrement", tnum );
	
	CurXInc = xInc;
	
	[ self display ];
	[ self resetSpinners ];
	
	return self;
}
	
/**********************************************************************/

- setYInc : sender
{
	float	yInc;
	
	yInc = [ sender floatValue ];

	if( sender == yIncSlider )
		[ yIncField setFloatValue : yInc ];
	else
	{
		if( yInc > MAXYINC )
		{
			yInc = MAXYINC;
			[ yIncField setFloatValue : yInc ];
		}
		else if( yInc < MINYINC )
		{
			yInc = MINYINC;
			[ yIncField setFloatValue : yInc ];
		}
		
		[ yIncSlider setFloatValue : yInc ];
	}
	
	sprintf( tnum, "%6.3f", yInc );
	NXWriteDefault( OWNER, "YIncrement", tnum );
	
	CurYInc = yInc;
	
	[ self display ];
	[ self resetSpinners ];
	
	return self;
}

/**********************************************************************/

- setNumSpins : sender
{
	int	numSpins;
	
	numSpins = [ sender intValue ];
	
	if( sender == numSpinSlider )
		[ numSpinField setIntValue : numSpins ];
	else
	{
		if( numSpins > MAXNUMSPINS )
		{
			numSpins = MAXNUMSPINS;
			[ numSpinField setIntValue : numSpins ];
		}
		else if( numSpins < MINNUMSPINS )
		{
			numSpins = MINNUMSPINS;
			[ numSpinField setIntValue : numSpins ];
		}
		
		[ numSpinSlider setIntValue : numSpins ];
	}
	
	sprintf( tnum, "%2d", numSpins );
	NXWriteDefault( OWNER, "NumberOfSpinners", tnum );
	
	CurNumSpins = numSpins;
	
	[ self display ];
	[ self resetSpinners ];
	
	return self;
}
	
/**********************************************************************/

- setRadius : sender
{
	int	radius;
	
	radius = [ sender intValue ];
	
	if( sender == radiusSlider )
		[ radiusField setIntValue : radius ];
	else
	{
		if( radius > MAXRADIUS )
		{
			radius = MAXRADIUS;
			[ radiusField setIntValue : radius ];
		}
		else if( radius < MINRADIUS )
		{
			radius = MINRADIUS;
			[ radiusField setIntValue : radius ];
		}
		
		[ radiusSlider setIntValue : radius ];
	}
	
	sprintf( tnum, "%3d", radius );
	NXWriteDefault( OWNER, "Radius", tnum );
	
	CurRadius = radius;
	
	[ self display ];
	[ self resetSpinners ];
	
	return self;
}
	
/**********************************************************************/

- setTailLength : sender 
{
	int	tailLen;
	
	tailLen = [ sender intValue ];
	
	if( sender == tailSlider )
		[ tailField setIntValue : tailLen ];
	else
	{
		if( tailLen > MAXTAILLEN )
		{
			tailLen = MAXTAILLEN;
			[ tailField setIntValue : tailLen ];
		}
		else if( tailLen < MINTAILLEN )
		{
			tailLen = MINTAILLEN;
			[ tailField setIntValue : tailLen ];
		}
		
		[ tailSlider setIntValue : tailLen ];
	}
	
	sprintf( tnum, "%3d", tailLen );
	NXWriteDefault( OWNER, "TailLength", tnum );
		
	CurTailLen = tailLen;
	
	[ self display ];
	[ self resetSpinners ];
	
	return self;
}

/**********************************************************************/

@end